/**
 * Created by Administrator on 2016/9/23.
 */


//头部效果
//关注我们的隐藏效果
$(".guanzhu").on({
    mouseover:function(){
        $(".guanzhu-bg").stop().fadeIn();
        $(".guanzhu-bg").show().stop().animate({
            top:25,
        },20);
    },
    mouseout:function(){
        $(".guanzhu-bg").stop().fadeOut();
        $(".guanzhu-bg").show().animate({
            top:33,
        },20);
    }
});


//购物车的隐藏效果
$("#shopCon").on({
    mouseover:function(){
        $(".shopYc").stop().slideDown(400)
    },
    mouseout:function(){
        $(".shopYc").stop().slideUp(400)
    }
});



//添加商品
var getGoods = function(){
    var $shopcart = $.cookie("shopcart");
    if(!$shopcart != undefined){
        var json = strOper.get($shopcart);
        var html = "";
        $.each(json,function(i,o){
            html += "<tr>"
            +"<td>"
            +"<input type=\"checkbox\" class=\"ck\" \ />"
            +"</td>"
            +"<td>"
            +"<img src=\""+o.img+"\" alt=\"\" \/>"
            +"</td>"
            +"<td>"
            +" "+o.intro+" "
            +"</td>"
            +"<td>"
            +"<button class=\"jia\" pid=\""+o.id+"\">+<\/button>"
            +"<p>"+o.num+"</p>"
            +"<button class=\"jian\" pid=\""+o.id+"\">-</button>"
            +"</td>"
            +"<td>"
            +"￥<span>"+o.price+"</span>"
            +"</td>"
            +"<td>"
            +"￥<span class='xiaoji'>"+o.price*o.num+"</span>"
            +"</td>"
            +"<td>"
            +"<button class=\"del\" pid=\""+o.id+"\">删除</button>"
            +"</td>"
            +"</tr>";

            $("#tbody").html(html);
        })
    }

}


//给删除   增加  减少添加事件
$("#tbody").click(function(e){
    if($(e.target).attr("class") === "del"){
        var $shopcart = $.cookie("shopcart");
        var $pid = $(e.target).attr("pid");
        var result = strOper.del($shopcart,$pid);
        $.cookie("shopcart",result);
        getGoods();
    }else if($(e.target).attr("class") === "jia"){
        var $count = $(e.target).next().html();
        var $shopcart = $.cookie("shopcart");

        var $pid = $(e.target).attr("pid");
        var result = strOper.counter($shopcart,$pid,1);
        var $price = $(e.target).parent().next().children().html();

        var $sum = eval($count) + 1;
        var $total =$price*$sum ;
        $(e.target).parent().next().next().children().html($total);
        $.cookie("shopcart",result);
        $(e.target).next().html($sum);

    }else if($(e.target).attr("class") === "jian"){
        var $count = $(e.target).prev().html();

        if($count == 1){
            $(e.target).prev().html("1");
        }else{
            var $shopcart = $.cookie("shopcart");

            var $pid = $(e.target).attr("pid");
            var result = strOper.counter($shopcart,$pid,-1);
            var $price = $(e.target).parent().next().children().html();

            var $sum = eval($count) - 1;
            var $total =$price*$sum ;
            $(e.target).parent().next().next().children().html($total);

            $.cookie("shopcart",result);
            $(e.target).prev().html($sum);
        }

    }



});



getGoods();